package com.czl.module_storehouse.activity.intostore.record;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.WarehouseRecordAdapter;
import com.czl.module_storehouse.bean.StorageRecordBean;
import com.czl.module_storehouse.databinding.ActivityWareHouseRecordBinding;
import com.czl.module_storehouse.event.WarehouseRecordEvent;
import com.czl.module_storehouse.mvp.presenter.WarehouseRecordPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class WarehouseRecordActivity extends BaseRecyclerViewActivity<ActivityWareHouseRecordBinding> implements SimpleView {

    private WarehouseRecordAdapter mAdapter;
    private final List<StorageRecordBean> mAllSortList = new ArrayList<>();
    @InjectPresenter
    WarehouseRecordPresenter mWarehouseRecordPresenter;

    @Override
    protected ActivityWareHouseRecordBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return ActivityWareHouseRecordBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("入库记录");
        mAdapter = new WarehouseRecordAdapter(R.layout.item_ware_house_record, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        initListener();
    }

    protected void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            StorageRecordBean item = mAdapter.getItem(position);
            goInfoRecordActivity(item);
        });
        mChildBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                search(s.toString());
            }
        });
    }

    private void search(String text) {
        if (TextUtils.isEmpty(text)) {
            mAdapter.setList(mAllSortList);
            return;
        }
        List<StorageRecordBean> newList = new ArrayList<>();
        for (StorageRecordBean bean : mAllSortList) {
            if (bean.getStorageModelStr() != null && bean.getStorageModelStr().contains(text)) {
                newList.add(bean);
            }
        }
        mAdapter.setList(newList);
        if (newList.isEmpty()) {
            View view = View.inflate(getContext(), R.layout.common_empty_layout, null);
            mAdapter.setEmptyView(view);
        }
    }

    private void goInfoRecordActivity(StorageRecordBean bean) {
        WarehouseRecordEvent event = new WarehouseRecordEvent();
        event.setStorageRecordBean(bean);
        EventBus.getDefault().postSticky(event);
        startActivity(new Intent(getContext(), RecordInfoActivity.class));
    }

    @Override
    protected void loadData() {
        super.loadData();
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    private void loadData(boolean refresh) {
        mWarehouseRecordPresenter.getStorageList(refresh);
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t != null) {
            ListBean<StorageRecordBean> list = (ListBean<StorageRecordBean>) t;
            if (list.getList() != null) {
                mAllSortList.clear();
                mAllSortList.addAll(list.getList());
            }
        }
        handleResponseData(mAdapter, mWarehouseRecordPresenter.getPageNo(), (ListBean<StorageRecordBean>) t);
    }
}
